//
// Created by Tan Ke on 2024/2/18.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int lengthOfLastWord(char* s)
{
    int endIndex = strlen(s) - 1;
    int startIndex = 0;
    bool beginCnt = false;

    if (strlen(s) == 0) {
        return 0;
    }

    for (int i = strlen(s) - 1; i >= 0; i --) {
        if (s[i] == ' ' && beginCnt == false) {
            continue;
        }
        if (s[i] != ' ' && beginCnt == false) {
            beginCnt = true;
            endIndex = i;
        }
        if (s[i] == ' ' && beginCnt == true) {
            startIndex = i + 1;
            break;
        }
    }

    if (beginCnt == false) {
        return 0;
    }

    return endIndex - startIndex + 1;
}


int main(void)
{
    printf("%d\n", lengthOfLastWord("Hello World"));
    printf("%d\n", lengthOfLastWord("   fly me   to   the moon  "));
    printf("%d\n", lengthOfLastWord("luffy is still joyboy"));

    return 0;
}
